module.exports = app => {
  var express = require('express');
  const oss = require('../../services/ossService');
  const tokens = require('../../services/tokenService');
  const { permission } = require('../../services/permission');
  const userController = require('../../controllers/admin/userController');
  const classController = require('../../controllers/admin/classController');
  const studentController = require('../../controllers/admin/studentController');
  const chapterController = require('../../controllers/admin/chapterController');
  const teacherController = require('../../controllers/admin/teacherController');
  const courseController = require('../../controllers/admin/courseController');
  const seriesController = require('../../controllers/admin/seriesController');
  const notifyController = require('../../controllers/admin/notifyController');
  const workController = require('../../controllers/admin/workController');
  const orgController = require('../../controllers/admin/orgController');

  var router = express.Router();

  router.post('/user/login', userController.login);
  router.get('/user/info', userController.info);
  router.post('/user/logout', userController.logout);

  router.all('*',function (req,res,next) {
    tokens.checkToken(req.header('x-token'), function (err,decode) {
      if (err)
        return res.send({code: 50014, message: '身份校验失败'});
      req.decode = decode;
      permission(req, res, next);
    })
  });

  router.get('/class', classController.list);
  router.post('/class', classController.create);
  router.post('/class/:id/student', classController.addStudent);
  router.delete('/class/:id', classController.delete);

  router.get('/teacher/list', teacherController.list);
  router.post('/teacher', teacherController.create);
  router.patch('/teacher/:id', teacherController.update);
  router.delete('/teacher/:id', teacherController.delete);


  router.post('/student/create', studentController.create);
  router.get('/student/list', studentController.list);

  router.get('/chapter/list', chapterController.list);
  router.get('/chapter/listBySeries', chapterController.listBySeries);
  router.post('/chapter/order',chapterController.order);
  router.post('/chapter/create',chapterController.create);
  router.post('/chapter/delete',chapterController.delete);
  router.post('/chapter/update',chapterController.update);

  router.get('/series/list', seriesController.list);
  router.post('/series/order',seriesController.order);
  router.post('/series/create',seriesController.create);
  router.post('/series/delete',seriesController.delete);
  router.post('/series/update',seriesController.update);

  router.get('/course/list', courseController.listByChapter);
  router.post('/course/order',courseController.order);
  router.post('/course/create',courseController.create);
  router.post('/course/delete',courseController.delete);
  router.post('/course/update',courseController.update);
  router.post('/course/open',courseController.open);

  router.post('/oss/getOssKey',oss.getOssKey);

  router.get('/file/list', workController.list);
  router.post('/file/upload', workController.upload);
  router.post('/file/delete', workController.delete);
  router.post('/file/update', workController.update);

  router.get('/notify/list',notifyController.list);
  router.post('/notify/create',notifyController.create);
  router.post('/notify/delete',notifyController.delete);

  router.get('/org', orgController.list);
  router.get('/org/:id', orgController.info);
  router.post('/org', orgController.create);
  router.patch('/org/:id/delay', orgController.delayExp);
  router.patch('/org/:id', orgController.update);
  router.post('/org/reset/:id', orgController.resetPwd);
  // router.delete('/org/:id', orgController.delete);

  app.use('/manager', router);
};
